﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Prism.Games.GalEngine.Scripts.FunctionModel;
using System.IO;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public static class ResourcePreLoader
    {
        public static void CacheScene(IScene scene)
        {
            if (scene is TrademarkScene)
            {
                var tscene = scene as TrademarkScene;
                CacheObject(tscene.BackgroundImage);
                // CacheObject(tscene.BackgroundMusic);
                CacheObject(tscene.BackgroundMusic);
                CacheObject(tscene.TrademarkImage);
            }
            else if (scene is MenuScene)
            {
                var mscene = scene as MenuScene;
                CacheObject(mscene.BackgroundImage);
                // CacheObject(mscene.BackgroundMusic);
                CacheObject(mscene.BackgroundMusic);
            }
            else if (scene is VideoScene)
            {
            }
            else if (scene is GamingScene)
            {
                var gscene = scene as GamingScene;
                CacheObject(gscene.BackgroundImage);
                // CacheObject(gscene.BackgroundMusic);
                CacheObject(gscene.BackgroundMusic);
                CacheObject(gscene.ExpressionImage);
                CacheObject(gscene.SystemSound);
                CacheObject(gscene.Vocal);

                foreach (var i in gscene.VerticalImages)
                {
                    CacheObject(i);
                }
            }
        }

        //public static void UnCacheScene(IScene scene)
        //{
        //    if (scene is TrademarkScene)
        //    {
        //        var tscene = scene as TrademarkScene;
        //        UnCacheObject(tscene.BackgroundImage);
        //        Helper.Parser.TrademarkBgmParser.Restart();
        //        UnCacheObject(tscene.BackgroundMusic);
        //        UnCacheObject(tscene.TrademarkImage);
        //    }
        //    else if (scene is MenuScene)
        //    {
        //        var mscene = scene as MenuScene;
        //        UnCacheObject(mscene.BackgroundImage);
        //        Helper.Parser.MenuBgmParser.Restart();
        //        UnCacheObject(mscene.BackgroundMusic);
        //    }
        //    else if (scene is VideoScene)
        //    {
        //    }
        //    else if (scene is GamingScene)
        //    {
        //        var gscene = scene as GamingScene;
        //        UnCacheObject(gscene.BackgroundImage);
        //        Helper.Parser.BgmParser.Restart();
        //        UnCacheObject(gscene.BackgroundMusic);
        //        UnCacheObject(gscene.ExpressionImage);
        //        //UnCacheObject(gscene.SystemSound);
        //        Helper.Parser.VocalParser.Restart();
        //        UnCacheObject(gscene.Vocal);

        //        foreach (var i in gscene.VerticalImages)
        //        {
        //            UnCacheObject(i);
        //        }
        //    }
        //}

        public static void CacheObject(ICacheable cacheableObject)
        {
            if (cacheableObject != null)
            {
                var uriContent = cacheableObject as IUriContent;
                if (uriContent.PartUri != null)
                {
                    if (Helper.CurrentCache.EnsureItem(uriContent))
                    {
                        uriContent.CacheStream = Helper.CurrentCache.GetItem(uriContent).Stream;
                        uriContent.IsCached = true;
                    }
                    else
                    {
                        var stream = Helper.Packer.GetPartStream(uriContent.PartUri);
                        if (stream == null)
                            return;

                        var cacheObject = new Parser.CacheObject();
                        cacheObject.Stream = stream;
                        cacheObject.Content = uriContent;
                        cacheableObject.CacheStream = cacheObject.Stream;
                        Helper.CurrentCache.Add(cacheObject);
                        cacheableObject.IsCached = true;
                    }
                }
            }
        }

        //public static void UnCacheObject(ICacheable cacheableObject)
        //{
        //    if (cacheableObject != null)
        //    {
        //        if (cacheableObject.CacheStream != null)
        //        {
        //            cacheableObject.CacheStream.Close();
        //            cacheableObject.IsCached = false;
        //        }
        //    }
        //}
    }
}
